<?php

class Site_Browse_Controller extends Base_Controller {

	public $restful = true;

	public function __construct()
	{
		parent::__construct();
		//--Your Code --//
		$this->theme->set_theme("default") ;
		$this->theme->set_layout('column3');
	}



	public function get_location($loc_slug){
		$data['mode'] = "LOC";
		$lid = $this->SlugID('locations',$loc_slug);
		//--Set Location --//
		Location::set($lid);
		$cid = 0;

		$data['category'] = Categories::findPK($cid);
		$data['location'] = Locations::findPK($lid);
		
		$data['listings'] = Terms::post($lid,$cid, $offset = 0 , $limit = 10);
		$data['listing_title'] = 'All listing of '.$data['location']->name;
		
		
		$listing_data = $data['listings'];
		
		$config = array();
		$config['minifyJS'] = TRUE;
		$config['map_type'] = 'ROADMAP';
		$config['trafficOverlay'] = 'TRUE';
		$config['map_height'] = '300px';
		$config['zoom'] = "auto";
		//$config['center'] = $long_lat;
		
		$config['adsense'] = FALSE;
		$config['adsensePublisherID'] = Settings::get('en','adsense_pub_id');
		$config['adsenseFormat'] = 'HALF_BANNER';
		$config['adsensePosition'] = 'BOTTOM_LEFT';
		
		$Gmap = new Gmaps();
		$Gmap->initialize($config);
		$m=1;
		if($listing_data){
			foreach ($listing_data as $map){
				$img_path = \Laravel\URL::base()."/public/map_icons/numeric/red".$m.".png";
				$long_lat = $map->lat.','.$map->long;
				$marker = array();
				$marker['icon'] = $img_path;
				$marker['position'] = $long_lat;
				$marker['infowindow_content'] = "<strong>$map->title</strong>"."</br>"."$map->address1"." $map->address1";
				$marker['animation'] = 'DROP';
				$Gmap->add_marker($marker);
				$m++;
			}
			$data['map'] = $Gmap->create_map();
				
		
		}
		
		
		/*-- META DATA */
		$this->theme->title($data['location']->meta_title.'-'.$this->location.' - '.Settings::get('en','site_title'));
		$this->theme->description($data['location']->meta_description);
		$this->theme->keyword($data['location']->meta_keyword);
		/*-- BREADCRUMB */

		Breadcrumb::addbreadcrumb('Home', URL::base());
		Breadcrumb::addbreadcrumb(Location::get(), URL::base());
		Breadcrumb::setSeparator('&raquo;');
		$data['breadcrumb'] = Breadcrumb::generate();

		return $this->theme->render('site::listing',$data);
	}

	public function get_category($loc_slug,$cat_slug){

		
		
		$data['mode'] = "CAT";
		$lid = $this->SlugID('locations',$loc_slug);
		
		
		$cid = $this->SlugID('categories',$cat_slug);

		

		//--IF NO LOCATION FOUND ---//
		if($lid){
			$data['location'] = Locations::findPK($lid);
		}else{

			$data['location'] = '0';
		}


		$data['category'] = Categories::findPK($cid);
		
		
		$data['listings'] = Terms::post($lid,$cid, $offset = 0 , $limit = 10);



		$parse_data = array(
				'location' => trim($this->location),
				'category' => trim($data['category']->name)
		);

		$listing_data = $data['listings'];
		
	
		$config = array();
		$config['minifyJS'] = TRUE;
		$config['map_type'] = 'ROADMAP';
		$config['trafficOverlay'] = 'TRUE';
		$config['map_height'] = '300px';
		$config['zoom'] = "auto";
		//$config['center'] = $long_lat;
		
		$config['adsense'] = FALSE;
		$config['adsensePublisherID'] = Settings::get('en','adsense_pub_id');
		$config['adsenseFormat'] = 'HALF_BANNER';
		$config['adsensePosition'] = 'BOTTOM_LEFT';
		
		$Gmap = new Gmaps();
		$Gmap->initialize($config);
		$m=1;
		if($listing_data){
			foreach ($listing_data as $map){
				$img_path = URL::base()."/public/map_icons/numeric/red".$m.".png";
				$long_lat = $map->lat.','.$map->long;
				$marker = array();
				$marker['icon'] = $img_path;
				$marker['position'] = $long_lat;
				$marker['infowindow_content'] = "<strong>$map->title</strong>"."</br>"."$map->address1"." $map->address1";
				$marker['animation'] = 'DROP';
				$Gmap->add_marker($marker);
				$m++;
			}
			$data['map'] = $Gmap->create_map();
			
	
		}
		$meta_title = Parser::parse_string(trim(Settings::get('en','seo_listing_title')), $parse_data);
		$meta_keyword = Parser::parse_string(trim(Settings::get('en','seo_listing_keyword')), $parse_data);
		$meta_description = Parser::parse_string(trim(Settings::get('en','seo_listing_description')), $parse_data);

		$this->theme->title($meta_title.' - '.Settings::get('en','site_title'));
		$this->theme->description($meta_description);
		$this->theme->keyword($meta_keyword);

		/*-- BREADCRUMB */

		$breadcrumb = Categories::breadcrumb($cid);
		
		Breadcrumb::addbreadcrumb('Home', URL::base());
		Breadcrumb::addbreadcrumb(Location::get(), URL::base().'/'.Location::get());
		foreach($breadcrumb as $br){
			Breadcrumb::addbreadcrumb($br->name, URL::base().'/'.Location::get().'/'.$br->slug);
		}
		Breadcrumb::setSeparator('&raquo;');
		

		$data['breadcrumb'] = Breadcrumb::generate();

		
		return $this->theme->render('site::listing',$data);
	}

	public function get_profile(){
		$this->theme->set_layout('column2');

		$id =  end(URI::$segments);

		if(!empty($id)){
			$post = Post::findPK($id);

			$data['post'] = $post;
			return $this->theme->render('site::profile',$data);
		}


	}

	public function get_all(){
		 
		$this->theme->set_theme("default") ;
		$this->theme->set_layout('column1');
		 
		$data['category_parent'] = Categories::where('parent_id','=',0)->get(array('id','name','slug'));
		 
		Breadcrumb::addbreadcrumb('Home', URL::base());
		Breadcrumb::addbreadcrumb('Category All', URL::base());
		Breadcrumb::setSeparator('&raquo;');
		
		$data['breadcrumb'] = Breadcrumb::generate();
		
		return $this->theme->render('site::widget.category_all',$data);
	}


}
